package cn.oms.manage.service.impl;

import cn.oms.commons.ex.ServiceException;
import cn.oms.commons.pojo.vo.PageData;
import cn.oms.commons.web.ServiceCode;
import cn.oms.manage.dao.persist.repository.IRecordRepository;
import cn.oms.manage.pojo.entity.Medicine;
import cn.oms.manage.pojo.entity.Record;
import cn.oms.manage.pojo.param.MedicineUpdateParam;
import cn.oms.manage.pojo.param.RecordAddNewParam;
import cn.oms.manage.pojo.param.RecordUpdateParam;
import cn.oms.manage.pojo.vo.MedicineListVO;
import cn.oms.manage.pojo.vo.MedicineStandardVO;
import cn.oms.manage.pojo.vo.RecordListVO;
import cn.oms.manage.pojo.vo.RecordStandardVO;
import cn.oms.manage.service.IRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * 说明：
 *
 * @author:Hyx
 * @date:2023/6/16
 **/
@Slf4j
@Service
public class RecordServiceImpl implements IRecordService {

    @Value("${oms.crud.default-query-page-size}")
    private Integer defaultQueryPageSize;

    @Autowired
    private IRecordRepository recordRepository;
    public RecordServiceImpl(){
        log.debug("创建业务类对象：RecordServiceImpl");
    }

    @Override
    public void addNew(RecordAddNewParam recordAddNewParam){
        log.debug("开始处理【添加诊断结果】的业务，参数：{}",recordAddNewParam);
        Record record=new Record();
        BeanUtils.copyProperties(recordAddNewParam,record);
        log.debug("即将执行插入数据，参数：{}", record);
        int rows = recordRepository.insert(record);
        if (rows != 1) {
            String message = "添加诊断结果失败！服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_INSERT, message);
        }
    }

    @Override
    public void renew(Long id){
        log.debug("开始处理【根据ID刷新诊断详情】的业务，参数：{}",id);
        recordRepository.renew(id);
    }


    public void delete(Long id){
        log.debug("开始处理【根据ID删除诊断记录】的业务，参数：{}",id);
        // 调用Mapper对象的getStandardById()执行查询
        RecordStandardVO record = recordRepository.getStandardById(id);
        log.debug("根据ID={}检查诊断数据是否存在，查询结果：{}", id, record);
        // 判断查询结果是否为null
        if (record == null) {
            String message = "删除诊断记录失败，尝试删除的诊断数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }

        recordRepository.deleteById(id);
    }

    @Override
    public void updateInfoById(Long id, RecordUpdateParam recordUpdateParam) {
        log.debug("开始处理【修改诊断详情】的业务，ID：{}，新数据：{}", id, recordUpdateParam);
        // 调用Mapper对象的getStandardById()执行查询
        RecordStandardVO queryResult = recordRepository.getStandardById(id);
        // 判断查询结果是否为null
        if (queryResult == null) {
            // 是：抛出异常
            String message = "修改诊断记录失败，尝试修改的诊断数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }

        // 创建Record对象
        Record record = new Record();
        // 复制属性，设置ID
        BeanUtils.copyProperties(recordUpdateParam, record);
        record.setId(id);
        // 调用Mapper对象的update()方法执行修改
        int rows = recordRepository.update(record);
        if (rows != 1) {
            String message = "修改诊断详情失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }
    @Override
    public RecordStandardVO getStandardById(Long id) {
        log.debug("开始处理【根据ID查询诊断详情】的业务，参数：{}", id);
        RecordStandardVO queryResult = recordRepository.getStandardById(id);
        if (queryResult == null) {
            String message = "查询诊断详情失败，诊断数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        return queryResult;
    }

    @Override
    public PageData<RecordListVO> list(Integer pageNum) {
        log.debug("开始处理【查询诊断列表】的业务，页码：{}", pageNum);
        PageData<RecordListVO> pageData = recordRepository.list(pageNum,defaultQueryPageSize);
        return pageData;
    }

    @Override
    public PageData<RecordListVO> list(Integer pageNum, Integer pageSize) {
        log.debug("开始处理【查询诊断列表】的业务，页码：{}，每页记录数：{}", pageNum, pageSize);
        PageData<RecordListVO> pageData = recordRepository.list(pageNum, pageSize);
        return pageData;
    }

//    @Override
//    public void renew(RecordAddNewParam recordAddNewParam){}
}
